package com.yinhai.contacts.util;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.yinhai.constant.Contact;
import com.yinhai.contacts.bean.PhoneContact;
import com.yinhai.contacts.bean.SameContactAda;
import com.yinhai.contacts.bean.SameRecord;
import com.yinhai.model.ContactsUser;
import com.yinhai.util.PhoneSearchFunction;
import com.yinhai.util.PinYinUtil;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;

public class ContactsUtils {
	public final static String CONTACT_KEY = "com.contact.intent.send";
	public static final Uri uri =android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
	public static final String[] projection = { 
		ContactsContract.CommonDataKinds.Phone._ID, //记录ID
		ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, //联系人姓名
		ContactsContract.CommonDataKinds.Phone.DATA1, //电话号码
		"sort_key", //拼音
		ContactsContract.CommonDataKinds.Phone.CONTACT_ID, //contactID
		ContactsContract.CommonDataKinds.Photo.PHOTO_ID, //头像ID
		ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID //raw_contactID每个联系人的唯一ID
		}; // 查询的列
	
	public static List<PhoneContact> _getContacts(ContentResolver cr,String str,int flag){
		List<PhoneContact> phoneContactList = null;
		Cursor cursor = null;
		String selection = "";
		try {
			
			if(flag==0){
				selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+ " = '"+str+"'";
			}else{
				selection = ContactsContract.CommonDataKinds.Phone.DATA1+ " = '"+str+"'";
			}
			
			cursor = cr.query(ContactsUtils.uri,projection,selection, null, "sort_key COLLATE LOCALIZED asc");
			if (cursor != null && cursor.getCount() > 0) {
				phoneContactList = new ArrayList<PhoneContact>();
				cursor.moveToFirst(); // 游标移动到第一项
				for (int i = 0; i < cursor.getCount(); i++) {
					cursor.moveToPosition(i);
					PhoneContact pc = new PhoneContact();
					pc.setId(cursor.getString(0));
					pc.setDisplay_name(cursor.getString(1));
					pc.setData1(cursor.getString(2));
					pc.setSort_key(cursor.getString(3));
					pc.setContact_id(cursor.getString(4));
					pc.setPhoto_id(cursor.getString(5));
					pc.setRaw_contact_id(cursor.getString(6));
					phoneContactList.add(pc);
				}
			}
			
		} catch (Exception e) {
			return phoneContactList;
		}finally{
			if(cursor!=null){
				cursor.close();
			}
		}
		return phoneContactList;
	}
	
	public static List<SameContactAda> getAdapterList(ContentResolver cr,List<String> lists,int flag){
		List<SameContactAda> scaList = null;
		scaList = new ArrayList<SameContactAda>();
		for(int i=0;i<lists.size();i++){
			String str = lists.get(i);
			List<PhoneContact> phoneContactList = ContactsUtils._getContacts(cr, str, flag);
			SameContactAda sca = new SameContactAda();
			List<SameRecord> sameRecordList = new ArrayList<SameRecord>();
			List<String> contacts_rawId_list = new ArrayList<String>();
			List<String> contacts_phone_list = new ArrayList<String>();
			for(int m=0;m<phoneContactList.size();m++){
				PhoneContact pc = new PhoneContact();
				pc = phoneContactList.get(m);
				if(m==0){
					sca.setFlag(String.valueOf(flag));
					sca.setMain_contacts_name(pc.getDisplay_name());
					sca.setMain_contacts_rawId(pc.getRaw_contact_id());
					SameRecord sr = new SameRecord();
					sr.setSameItem(pc.getDisplay_name());
					sr.setMomeItem(pc.getData1());
					sameRecordList.add(sr);
					contacts_phone_list.add(pc.getData1());
				}else{
					SameRecord sr = new SameRecord();
					sr.setSameItem(pc.getDisplay_name());
					sr.setMomeItem(pc.getData1());
					sameRecordList.add(sr);
					contacts_rawId_list.add(pc.getRaw_contact_id());
					contacts_phone_list.add(pc.getData1());
				}
			}
			sca.setSameRecordList(sameRecordList);
			sca.setContacts_rawId_list(contacts_rawId_list);
			sca.setContacts_phone_list(contacts_phone_list);
			scaList.add(sca);
		}
		return scaList;
	}
	
	public static LinkedList<String> _getContactsPhoneOrName(ContentResolver cr,int flag){
		LinkedList<String> phoneContactList = null;
		Cursor cursor = null;
//		Uri uri1 = Uri.parse("content://com.android.contacts/raw_contacts");
		Uri uri1 = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // 联系人Uri；
		// 查询的字段
		String[] projection1 = { ContactsContract.CommonDataKinds.Phone._ID,
				ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
				ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key",
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
				ContactsContract.CommonDataKinds.Phone.PHOTO_ID,
				ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
				ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID
				};
		try {
			//String selection = ContactsContract.CommonDataKinds.Phone.DELETED+ " = 0";
				// 按照sort_key升序查詢
			cursor = cr.query(uri1, projection1, " _id  in (select max(data_id) from phone_lookup group by raw_contact_id)", null, "sort_key COLLATE LOCALIZED asc");
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst(); // 游标移动到第一项
				phoneContactList = new LinkedList<String>();
				for (int i = 0; i < cursor.getCount(); i++) {
					cursor.moveToPosition(i);
					if(flag==0){
						phoneContactList.add(cursor.getString(1));
					}else{
						phoneContactList.add(cursor.getString(2));
					}
					
				}
			}
			
			
		} catch (Exception e) {
			return phoneContactList;
		}finally{
			if(cursor!=null){
				cursor.close();
			}
		}
		return phoneContactList;
	}
	
	//查询联系人中姓名或电话号码相同的数据
	public static List<String> CheckOverlap(LinkedList<String> src) {
		List<String> rtn = new ArrayList<String>();
		if(src == null) {
			return rtn;
		}
		boolean isOne = true;
		boolean isOverlap = false;
		int i = 0;
		while(src.size() > 1) {
			String element = src.removeFirst();
			if(isOne) {
				if(element == null || "".equals(element)) {
					continue;
				}
			}
			isOverlap = false;
			for(i = 0; i < src.size();) {
				String eleStr = src.get(i);
				if(isOne) {
					if(eleStr == null || "".equals(eleStr)) {
						src.remove(i);
						continue;
					}
				}
				if(eleStr.equalsIgnoreCase(element)) {
					isOverlap = true;
					src.remove(i);
					continue;
				} else {
					++i;
				}
			}
			if(isOverlap) {
				rtn.add(element);
			}
			if(isOne) {
				isOne = false;
			}
		}
		return rtn;
	}

}
